package bookpad.page;

import java.io.IOException;

import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import scenic3.annotation.ActionPath;
import scenic3.annotation.Default;
import scenic3.annotation.Page;
import scenic3.annotation.Var;
import bookpad.model.ResultSet;
import bookpad.model.UserBook;
import bookpad.service.AmazonService;
import bookpad.service.UserDataService;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Page("/info")
public class InfoPage extends BasePage {

    private AmazonService amazonService = new AmazonService();
    private UserDataService us = new UserDataService();

    @ActionPath("logon/{ASIN}")
    public Navigation logon(@Var("ASIN") String ASIN) {
        if (getUser().isAuthentication()) {
            return redirect("/info/detail/" + ASIN);
        } else {
            try {
                UserService userService = UserServiceFactory.getUserService();
                response.sendRedirect(userService.createLoginURL(request
                    .getRequestURI()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }

    @ActionPath("ranking/{category}")
    public Navigation ranking(@Var("category") String category) {
        String itemPage = requestScope("itemPage");

        if (isNull(category))
            category = "本";

        ResultSet rs = amazonService.searchList(category, null, itemPage);

        requestScope("category", category);
        requestScope("itemPage", itemPage);
        requestScope("itemList", rs.getItemList());
        requestScope("TotalPages", rs.getTotalPages());
        requestScope("TotalResults", rs.getTotalResults());

        sessionScope("categorys", categorys);

        return forward("/info/ranking.jsp");
    }

    @ActionPath("search")
    public Navigation search() {
        String keywords = requestScope("keywords");
        String itemPage = requestScope("itemPage");

        if (!isNull(keywords)) {
            ResultSet rs = amazonService.searchList(keywords, null, itemPage);
            requestScope("itemList", rs.getItemList());
            requestScope("TotalPages", rs.getTotalPages());
            requestScope("TotalResults", rs.getTotalResults());
        }
        return forward("/info/search.jsp");
    }

    @ActionPath("detail/{ASIN}")
    public Navigation detail(@Var("ASIN") String ASIN) throws Exception {
        ResultSet rs = amazonService.searchList(null, ASIN, null);
        if (!isEmpty(rs.getItemList()))
            requestScope("item", rs.getItemList().get(0));

        UserBook ub = us.getBook(getUser().getUserId(), ASIN);
        requestScope("userBook", ub);
        return forward("/info/detail.jsp");
    }

    @ActionPath("entry/{ASIN}")
    public Navigation entry(@Var("ASIN") String ASIN) {
        String status = requestScope("status");
        if (isNull(status))
            status = "to_read";

        UserBook ub = us.getBook(getUser().getUserId(), ASIN);
        if (isNull(ub)) {
            ub = new UserBook();
            ub.setUserId(getUser().getUserId());
            ub.setASIN(ASIN);
        }
        ub.setStatus(status);
        Datastore.put(ub);
        return redirect("/info/detail/" + ASIN);
    }

    @Default
    public Navigation index() {
        return redirect("ranking");
    }

}
